<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="projectName">
            <el-input v-model="form.projectName" :placeholder="$t('项目名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <el-input v-model="form.fundName" :placeholder="$t('基金名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="investmentTime">
            <el-date-picker
              v-model="form.investmentTime"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('投资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资金额') + $t('金额单位')" prop="investmentAmount">
            <Money v-model="form.investmentAmount" unit="w" :placeholder="$t('投资金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('初始持股比例(%)')" prop="initialEquityRatio">
            <Ratio v-model="form.initialEquityRatio" :placeholder="$t('初始持股比例')" :controls="false" :max="100" :min="0" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最新持股比例(%)')" prop="latestEquityRatio">
            <Ratio v-model="form.latestEquityRatio" :placeholder="$t('最新持股比例')" :controls="false" :max="100" :min="0" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('退出金额') + $t('金额单位')" prop="quitAmount">
            <Money v-model="form.quitAmount" unit="w" :placeholder="$t('退出金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('目前持有价值') + $t('金额单位')" prop="currentHoldValue">
            <Money v-model="form.currentHoldValue" unit="w" :placeholder="$t('目前持有价值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('MOIC')" prop="moic">
            <span slot="label">
              {{ $t('MOIC') }}
              <el-tooltip placement="top">
                <div slot="content">
                  MOIC = (退出金额 + 目前持有价值) / 投资金额，四舍五入保留4位小数，若投资金额等于0则MOIC也等于0
                </div>
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <el-input-number v-model="form.moic" :placeholder="$t('MOIC')" :controls="false" :precision="4" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('DPI')" prop="dpi">
            <span slot="label">
              {{ $t('DPI') }}
              <el-tooltip placement="top">
                <div slot="content">
                  DPI = 退出金额 / 投资金额，四舍五入保留4位小数，若投资金额等于0则DPI也等于0
                </div>
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <el-input-number v-model="form.dpi" :placeholder="$t('dpi')" :controls="false" :precision="4" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('IRR')" prop="irr">
            <Money v-model="form.irr" :placeholder="$t('IRR')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目简介')" prop="projectDescription">
            <el-input v-model="form.projectDescription" type="textarea" :placeholder="$t('项目简介')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gp:votedProject" />
    </div>
  </div>
</template>

<script>
    import { saveGpVotedProject, getGpVotedProject, calculate } from '@/api/fund/gp/votedProject/gpVotedProject'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  projectName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                }
            }
        },
      watch: {
        'form.investmentAmount': {
          handler(newValue) {
            if (newValue === '' || newValue === undefined) {
              this.form.investmentAmount = 0
            }
            this.calculate(this.form)
          },
          immediate: true
        },
        'form.quitAmount': {
          handler(newValue) {
            if (newValue === '' || newValue === undefined) {
              this.form.quitAmount = 0
            }
            this.calculate(this.form)
          },
          immediate: true
        },
        'form.currentHoldValue': {
          handler(newValue) {
            if (newValue === '' || newValue === undefined) {
              this.form.currentHoldValue = 0
            }
            this.calculate(this.form)
          },
          immediate: true
        }

      },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getGpVotedProject(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    gpId: this.$route.query.gpId,
                    projectName: undefined,
                    fundName: undefined,
                    investmentTime: undefined,
                    investmentAmount: 0,
                    initialEquityRatio: 0,
                    latestEquityRatio: 0,
                    quitAmount: 0,
                    currentHoldValue: 0,
                    moic: 0,
                    dpi: 0,
                    irr: undefined,
                    projectDescription: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveGpVotedProject(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 计算MOIC与DPI
          calculate(data) {
            calculate(data).then(response => {
              this.form.moic = response.data.moic
              this.form.dpi = response.data.dpi
            })
          }

        }

    }
</script>
